import cv2
import mediapipe as mp
import pyautogui
import numpy as np

screenWidth, screenHeight = pyautogui.size()

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
    static_image_mode=False,
    max_num_hands=2,
    min_detection_confidence=0.75,
    min_tracking_confidence=0.75)


def shibie_wuzhi_zhuangtai(hand_dict):
    finger_list = []
    for finger_poindts in [[2, 3, 4], [6, 7, 8], [10, 11, 12], [14, 15, 16], [18, 19, 20]]:
        x, y, z = finger_poindts
        #
        finger_one = hand_dict[x].y > hand_dict[y].y and hand_dict[y].y > hand_dict[z].y
        finger_list.append(finger_one)
    return finger_list


cap = cv2.VideoCapture(0)
history_list = []
while True:
    ret, frame = cap.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # 因为摄像头是镜像的，所以将摄像头水平翻转
    # 不是镜像的可以不翻转
    frame = cv2.flip(frame, 1)
    results = hands.process(frame)
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    # if results.multi_handedness:
    #     for hand_label in results.multi_handedness:
    #         print(hand_label)

    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            # 识别状态
            hand_dict = {i: v for i, v in enumerate(hand_landmarks.landmark)}
            # finger_table = shibie_wuzhi_zhuangtai(hand_dict)
            history_list.append(hand_dict)

            if len(history_list) >= 2:
                x_y_z = [[[d.x, d.y, d.z] for j, d in i.items()] for i in history_list]
                x_y_z = np.mean(np.array(x_y_z), 1)
                x_m, y_m, z_m = (np.max(x_y_z, 0) - np.min(x_y_z, 0)) / np.mean(x_y_z, 0)
                x_rl, y_ud, z_io = np.sum(x_y_z[1:] - x_y_z[:-1], 0)

                if x_m > 0.5:

                    if x_rl < 0:
                        print("left", x_rl)
                    else:
                        print("right", x_rl)
                elif y_m > 0.5:
                    if y_ud < 0:
                        print("up", y_ud)
                    else:
                        print("down", y_ud)

                elif np.abs(z_m) > 0.5:
                    if z_io < 0:
                        print("in", z_io)
                    else:
                        print("out", z_io)
                # print("可翻译指令",x_m,y_m,z_m)
                history_list = []

            mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
            # break
    cv2.imshow('MediaPipe Hands', frame)
    if cv2.waitKey(1) & 0xFF == 27:
        break
cap.release()

if __name__ == '__main__':
    pass
